package com.ycp.erp.view;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.one.common.utils.ImageFactory;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.widget.ClearEditView;
import com.ycp.erp.R;

import org.jetbrains.annotations.NotNull;

import androidx.fragment.app.DialogFragment;
import butterknife.ButterKnife;

/**
 *
 */
public class HintDialog extends DialogFragment implements View.OnClickListener {
    TextView tvTitle;
    TextView tvCancel;
    TextView tvConfirm;
    ClearEditView ctInput;
    private Activity mActivity;
    private String hint;
    private String title;
    private int inputType;
    private TextView tvContent;
    private View rlInput;
    private String content;
    private int type;//1 普通提示框 0 输入框

    public static HintDialog getInstance() {
        HintDialog dialog = new HintDialog();
        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
    }

    @NotNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = View.inflate(mActivity, R.layout.dialog_input, null);
        Dialog dialog = new Dialog(mActivity, R.style.PXDialog);
        ButterKnife.bind(view);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        Window alertWindow = dialog.getWindow();
        alertWindow.setGravity(Gravity.CENTER);
        //初始化组件
        initView(view);
//        WindowManager.LayoutParams params = alertWindow.getAttributes();
//        params.width = DensityUtil.dp2px(mActivity, 300);
//        alertWindow.setAttributes(params);

        return dialog;
    }
    public void setTitle(String title) {
        this.title=title;
    }
    public void setInputHint(String hint) {
        this.hint = hint;
    }

    /**
     * 输入框限制类型
     * @param type 1 手机号 2 邮箱
     */
    public void setInputType(int type){
        inputType=type;

    }
    public void setHintMsg(String title,String content){
        type=1;
        this.title=title;
        this.content=content;

    }
    private void initView(View view) {
        tvTitle=view.findViewById(R.id.tvTitle);
        tvCancel=view.findViewById(R.id.tvCancel);
        tvContent=view.findViewById(R.id.tvContent);
        tvConfirm=view.findViewById(R.id.tvConfirm);
        ctInput=view.findViewById(R.id.ctInput);
        rlInput=view.findViewById(R.id.rlInput);
        tvConfirm.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        tvTitle.setText(title);
        ctInput.setHint(hint);
        if (inputType==1){
            ctInput.setInputType(InputType.TYPE_CLASS_PHONE);
        }else if (inputType==2){
            ctInput.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        }else {
            //默认为普通文本
            ctInput.setInputType(InputType.TYPE_CLASS_TEXT);
        }
        if (type==1){
            tvContent.setText(content);
            tvContent.setVisibility(View.VISIBLE);
            rlInput.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvCancel:
                dismiss();
                break;
            case R.id.tvConfirm:
                if (type!=1&&StringUtils.isBlank(ctInput.getText().toString())) {
                    Toaster.showShortToast(hint);
                    return;
                }
                if (listener != null) {
                    listener.onConfirm(ctInput.getText().toString());
                    dismiss();
                }
                break;
            default:
                break;
        }
    }


    public void setInputListener(InputListener listener) {
        this.listener = listener;
    }

    private InputListener listener;

    public interface InputListener {
        void onCancel();

        void onConfirm(String data);

    }

}
